package com.answer.controller;


import com.answer.model.dto.user.*;
import com.answer.model.entity.User;
import com.answer.model.reslut.Result;
import com.answer.model.reslut.ResultPage;
import com.answer.model.vo.UserVo;
import com.answer.service.UserService;
import com.answer.utils.Redisutils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/user")
public class LogInController {

    @Resource
    private UserService userService;
    @Resource
    private HttpSession httpSession;

    @Resource
    private Redisutils redisutils;


    // 查询列表
    @PostMapping("/query/all")
    public Result<ResultPage<UserVo>> queryAll(@RequestBody UserQueryDTO userQueryDTO) {
        ResultPage<UserVo> userVoResultPage = userService.queryAll(userQueryDTO);
        return Result.success(userVoResultPage);
    }


    // 根据id查询
    @GetMapping("/query/one/{id}")
    public Result<UserVo> queryOneById(@PathVariable Long id) {
        UserVo userVo = userService.queryOneById(id);
        return Result.success(userVo);
    }


    // 用户登录
    @PostMapping("/login")
    public Result<UserVo> login(@RequestBody UserLoginDTO userLoginDTO, HttpSession session) {
        UserVo userVo = userService.login(userLoginDTO);
        Result<UserVo> success = Result.success(userVo);
        // String key = userVo.getUserAccount()+"-"+UUID.randomUUID();
        // redisutils.setToken(key,userVo);
        // success.setToken(key);
        session.setAttribute("userVo",userVo);
        return success;
    }

    // 用户注册
    @PostMapping("/register")
    public Result<UserVo> register(@RequestBody UserRegisterDTO userRegisterDTO) {
        UserVo register = userService.register(userRegisterDTO);
        return Result.success(register);
    }

    // 修改
    @PostMapping("/update")
    public Result update(@RequestBody UserUpdateDTO userUpdateDTO) {
        Boolean res = userService.update(userUpdateDTO);
        return res ? Result.success("修改成功") : Result.faile("修改失败");
    }

    // 退出登录
    @GetMapping("/logout")
    public Result logout(HttpServletRequest request) {
        // String token = request.getHeader("token");
        // redisutils.deleteKey(token);
        HttpSession session = request.getSession();
        session.removeAttribute("userVo");
        return Result.success();
    }


    // 获取登录用户
    @GetMapping("/get/login")
    public Result<UserVo> getLoginUser(HttpServletRequest request) {

        // reids 获取用户
        // String token = request.getHeader("token");
        // UserVo userVo = (UserVo) redisutils.get(token);

        // session获取用户
        HttpSession session = request.getSession();
        UserVo userVo = (UserVo) session.getAttribute("userVo");
        return Result.success(userVo);
    }

    @GetMapping
    public Result deleteUser(Long id) {
        boolean res = userService.removeById(id);
        return res ? Result.success("删除成功") : Result.faile("删除失败");
    }


}
